//
//  Singleton.m
//  GraseBook
//
//  Created by Alex on 06.08.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Singleton.h"


@implementation Singleton
@synthesize count_product_in_basket;
@synthesize user_login;
@synthesize user_name;
@synthesize user_second_name;
@synthesize user_id;
@synthesize neadCheckLogin;
@synthesize user_adress;
@synthesize user_city;
@synthesize user_id_country;
@synthesize user_id_zone;

@synthesize text_payment;
@synthesize text_payment_db;

NSMutableArray *array;

static Singleton * sharedMySingleton = NULL;
+(Singleton *)sharedMySingleton {
    if (!sharedMySingleton || sharedMySingleton == NULL) {
        sharedMySingleton = [Singleton new];
    }
    return sharedMySingleton;
}



-(UIColor*)backgroundTableView{
    UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
    [[UIImage imageNamed:@"background"] drawInRect:[UIScreen mainScreen].bounds];
    UIImage *bg_img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:bg_img ];
}



-(void)setBadge{
    int count_in_basket=0;
    NSArray *count_all = [SQLiteAccess selectManyRowsWithSQL:@"SELECT count FROM Basket"];
    
    if ([count_all count]<1) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate setBasketBadgeValue:0];
        return;
    }
    
    //NSLog(@"count_all  = %@",count_all);
    
    for (int i=0; i<[count_all count];i++) {
        count_in_basket = count_in_basket + [[[count_all objectAtIndex:i] objectForKey:@"count"] integerValue];
    }
    
    
    
    count_product_in_basket = count_in_basket;
    if (count_product_in_basket>0) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate setBasketBadgeValue:count_in_basket];
    }
}


-(void)checkUser{
    
    if ([[UserDefaults objectForKey:@"id_user"] integerValue]>0) {
        [Singleton sharedMySingleton].user_login = 1;
        [Singleton sharedMySingleton].user_name = [UserDefaults objectForKey:@"f_name_user"];
        [Singleton sharedMySingleton].user_second_name = [UserDefaults objectForKey:@"s_name_user"];
        [Singleton sharedMySingleton].user_id = [[UserDefaults objectForKey:@"id_user"] integerValue];
        
        
        [Singleton sharedMySingleton].user_adress = [UserDefaults objectForKey:@"u_adress"];
        [Singleton sharedMySingleton].user_city = [UserDefaults objectForKey:@"u_city"];
        [Singleton sharedMySingleton].user_id_country = [UserDefaults objectForKey:@"u_id_country"];
        [Singleton sharedMySingleton].user_id_zone = [UserDefaults objectForKey:@"u_id_zone"];
        
        
        
        //NSLog(@"1 - %@",[UserDefaults objectForKey:@"f_name_user"]);
        //NSLog(@"2 - %@",[UserDefaults objectForKey:@"s_name_user"]);
        //NSLog(@"3 - %i",[[UserDefaults objectForKey:@"id_user"] integerValue]);
        
    }else{
        [Singleton sharedMySingleton].user_login = 0;
    }


}

@end
